<?php namespace Home\Controller;
use Hdphp\Controller\Controller;
class ReController extends Controller{
//	private $app;
	public function index(){
//		p($_SESSION['cart']);
		
	    $num=array();
		$bs=explode(',', $_POST['bs']);
		$as=explode(',',$_POST['as']);
//		p($as);p($bs);
		foreach($bs as $k=>$v){
			$num[$v][]=$k;
		}
//		p($num);
		$num2=array();
		foreach($num as $k=>$v){
			foreach($v as $ka=>$va){
				$num2[$k][]=$as[$va];
			}
			
		}
//		p($num2);
		
//		p($as);
//		p($num);
//		p($num2);
		$brandModel=new \Common\Model\Brand;
//		p($_SESSION['cart']);die;
		//将bid转换为相应的bname
		$sum=array();
		$se=$_SESSION['cart'];
		foreach ($se['goods'] as $key => $value) {
			$sum=$se['goods'][$key]['options']['bid'];
			$bname=$brandModel->where(array('bid'=>$sum))->pluck('bname');
			$se['goods'][$key]['options']['bid']=$bname;
		}
//		p($se);
		//重组数组，组合成带有店铺名的形式
		$arr=array();
		foreach ($se['goods'] as $key => $value) {
//			if()
			$arr[$value['options']['bid']][]=$se['goods'][$key];
		}
//		p($arr);
		//将新组成的数组显示在页面上
		$last=array();
		foreach($num2 as $k=>$v){
			foreach ($v as $key => $value) {
				$last[$k][$value]=$arr[$k][$value];
			}
		}
		//统计商品的总数
		$num=0;
		foreach($last as $k=>$v){
			$num+=count($last[$k]);
		}
		View::with('num',$num);
//		p($num);
		
//		p($last);
		//算总价
		$total=0;
		foreach($last as $v){
			foreach ($v as $key => $value) {
				$total+=$value['total'];
			}
		}
		//所有的商品id
		$id=array();
		foreach($last as $k=>$v){
			foreach($v as $val){
				$id[]=$val['id'];
			}
		}
		$id=implode(',', $id);
		View::with('id',$id);
		//所有货品ID
		$listID=array();
		foreach($last as $k=>$v){
			foreach($v as $val){
				$listID[]=$val['options']['listID'];
			}
		}
		$listID=implode(',', $listID);
		View::with('listID',$listID);
		//获取所有的货号
		$nus=array();
		foreach($last as $k=>$v){
			foreach($v as $val){
				$nus[]=$val['options']['nus'];
			}
		}
		$nus=implode(',', $nus);
		View::with('nus',$nus);
//		p($listID);p($nus);
		View::with('total',$total);
//		p($total);die;
		View::with('last',$last);

		View::make();	
	}
	
}

 ?>